import {
  page,
  insertOrUpdate,
  del,
  updateStatus,
} from '../services/expense';
import React from 'react';
import {DatePicker, Input, message, Select, Tooltip} from 'antd';
const { MonthPicker, RangePicker } = DatePicker;
const { Option } = Select;

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '报销人',
    dataIndex: 'expMan',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '报销金额',
    dataIndex: 'expAmount',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '报销时间',
    dataIndex: 'expTime',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '报销摘要',
    dataIndex: 'expSummary',
    width: '180px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '报销类型',
    dataIndex: 'expType',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '单据数量',
    dataIndex: 'billCount',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: '120px',
    editable: true,
    render: text => {
      switch (text) {
        case 'ENTRY':
          return <span>生成</span>;
        case 'CANCEL':
          return <span>作废</span>;
        case 'APPLY':
          return <span>申请</span>;
        case 'PASS':
          return <span>通过</span>;
        case 'LEADER_PASS':
          return <span>运营主管通过</span>
        case 'PAY_ALLOW':
          return <span>允许支付</span>
        case 'PAY':
          return <span>支付</span>
        default:
          return <span />;
      }
    },
  },
  {
    title: '驳回理由',
    dataIndex: 'rejectCase',
    width: '120px',
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 5,
        label: '报销人',
        name: 'expMan',
        component: <Input placeholder="请输入报销人" />,
      },
      {
        md: 5,
        label: '摘要',
        name: 'expSummary',
        component: <Input placeholder="请输入摘要" />,
      },
      {
        md: 5,
        label: '状态',
        name: 'status',
        component: <Select placeholder="请选择状态">
          <Option value=""> </Option>
          <Option value="ENTRY">生成</Option>
          <Option value="APPLY">申请</Option>
          <Option value="PASS">通过</Option>
          <Option value="LEADER_PASS">运营主管通过</Option>
          <Option value="PAY_ALLOW">允许支付</Option>
          <Option value="PAY">支付</Option>
        </Select>
      },
      {
        md: 8,
        label: '报销时间',
        name: 'createTimeArray',
        component: <RangePicker format="YYYY-MM-DD HH:mm:ss" />,
      },
    ],
  },
];


export default {
  namespace: 'expense',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    searchValuesA: {
      status: {
        name: 'status',
        value: 'APPLY',
      }
    },
    searchValuesAl: {
      status: {
        name: 'status',
        value: 'PASS',
      }
    },
    searchValuesPa: {
      status: {
        name: 'status',
        value: 'LEADER_PASS',
      }
    },
    searchValuesP: {
      status: {
        name: 'status',
        value: 'PAY_ALLOW',
      }
    },
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(page, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(insertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(del, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *send({ payload }, { call, put }) {
      const res = yield call(updateStatus, payload);
      yield  put({
        type: 'save_r',
        payload: res,
      })
    },
    *reject({ payload }, { call, put }) {
      const res = yield call(insertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      })
    }
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0, status: 'ENTRY' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    handleSearchChangeAl(state, action) {
      const { searchValuesAl } = state;
      const newSearchForm = {
        ...searchValuesAl,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesA: newSearchForm,
      };
    },
    handleSearchChangeA(state, action) {
      const { searchValuesA } = state;
      const newSearchForm = {
        ...searchValuesA,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesA: newSearchForm,
      };
    },
    handleSearchChangePa(state, action) {
      const { searchValuesPa } = state;
      const newSearchForm = {
        ...searchValuesPa,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesPa: newSearchForm,
      };
    },
    handleSearchChangeP(state, action) {
      const { searchValuesP } = state;
      const newSearchForm = {
        ...searchValuesP,
        ...action.payload,
      };
      return {
        ...state,
        searchValuesP: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
          selectionIds: []
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },
  },
};
